<template>
  <div class="project-edit-container">
    <!-- 左侧导航菜单 -->
    <div class="left-menu">
      <div v-for="section in menuSections" :key="section.key" class="menu-section" >
        <div class="menu-header" @click="toggleSection(section.key)">
          <h2>{{ section.title }}</h2>
          <i class="el-icon-arrow-down" :class="{ 'rotate-180': !expandedSections[section.key] }"></i>
        </div>
        <div class="menu-items" v-show="expandedSections[section.key]">
          <div 
            v-for="item in section.items" 
            :key="item.key"
            class="menu-item" 
            :class="{ active: currentComponent === item.key }"
            @click="handleMenuClick(item)"
          >
            <span>{{ item.name }}</span>
          </div>
        </div>
      </div>
    </div>

    <!-- 右侧内容展示区域 -->
    <div class="right-content">
      <div class="content-panel">
        <div class="content-header">
          <h2>{{ currentSection }}</h2>
          <p>{{ currentDescription }}</p>
        </div>
        
        <div class="component-area">
          <component 
            :is="currentComponentMap[currentComponent]"
            v-if="currentComponent && currentComponentMap[currentComponent]"
          />
          <div v-else class="placeholder-content">
            <i class="el-icon-document"></i>
            <p>请选择左侧菜单项查看详细内容</p>
          </div>
        </div>
      </div>
    </div>

    <!-- AI助手触发按钮 -->
    <div class="ai-trigger-fixed" @click="showDrawer = true">
      <div class="ai-trigger-button">
        <div class="ai-icon">
          <i class="el-icon-chat-dot-round"></i>
        </div>
        <div class="ai-text">AI助手</div>
      </div>
    </div>

    <!-- AI助手抽屉 -->
    <el-drawer v-model="showDrawer" direction="rtl" size="400px" :with-header="false">
      <div class="drawer-content">
        <div class="drawer-header">
          <div class="ai-icon">
            <i class="el-icon-chat-dot-round"></i>
          </div>
          <div class="drawer-title">
            <h3>AI 智能助手</h3>
            <p class="ai-description">自定义的智能助手，有什么可以解决的问题？</p>
          </div>
          <div class="drawer-close" @click="showDrawer = false">
            <i class="el-icon-close"></i>
          </div>
        </div>
        
        <div class="chat-container">
          <div 
            v-for="(msg, index) in messages" 
            :key="index"
            class="message" 
            :class="msg.type + '-message'"
          >
            <div class="message-header">
              <span class="sender">{{ msg.sender }}</span>
            </div>
            <div class="message-content">
              {{ msg.content }}
            </div>
          </div>
        </div>
        
        <div class="input-area">
          <el-input 
            v-model="inputMessage" 
            placeholder="请输入您的问题..." 
            @keyup.enter="sendMessage" 
            type="textarea"
          />
          <el-button type="primary" @click="sendMessage" class="send-button">
            发送
          </el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script setup name="ProjectInfo">
import projectForm from "@/views/business/project/items/ProjectForm"

import projectSystem from "@/views/business/project/items/xmbj/projectSystem"
import nationalPolicy from "@/views/business/project/items/xmbj/nationalPolicy"
import industryDevelopment from "@/views/business/project/items/xmbj/industryDevelopment"
import painPoints from "@/views/business/project/items/xmbj/painPoints"
import expectedGoals from "@/views/business/project/items/xmbj/expectedGoals"
import patentQuery from "@/views/business/project/items/xmbj/patentQuery"

import functionalAnalysis from "@/views/business/project/items/gnfx/functionalAnalysis"
import componentAnalysis from "@/views/business/project/items/gnfx/componentAnalysis"
import componentInteraction from "@/views/business/project/items/gnfx/componentInteraction"
import functionalModel from "@/views/business/project/items/gnfx/functionalModel"
import interactionAnalysis from "@/views/business/project/items/gnfx/interactionAnalysis"
import functionalFlaw from "@/views/business/project/items/gnfx/functionalFlaw"



// 响应式数据
const showDrawer = ref(false)
const inputMessage = ref('')
const currentComponent = ref('projectForm')

// 菜单配置数据 
const menuConfig = {
  problem: {
    title: '项目背景',
    items: [
      { key: 'projectForm', name: '基本信息',  description: '基本信息' },
			{ key: 'projectSystem', name: '项目信息',  description: '项目信息' },
      { key: 'nationalPolicy', name: '国家政策',  description: '查看和分析国家相关政策' },
      { key: 'industryDevelopment', name: '行业发展',  description: '分析行业发展趋势' },
      { key: 'painPoints', name: '项目痛点',  description: '识别和分析项目痛点' },
      { key: 'patentQuery', name: '专利查询',  description: '查询相关专利信息' },
      { key: 'expectedGoals', name: '预期目标',  description: '设定项目预期目标' }
    ]
  },
  system: {
    title: '功能分析',
    items: [
      { key: 'functionalAnalysis', name: '功能分析列表',  description: '进行系统功能基本信息分析' },
      { key: 'componentAnalysis', name: '组件分析',  description: '分析系统组件' },
      { key: 'componentInteraction', name: '相互作用分析',  description: '分析组件间相互作用' },
      { key: 'functionalModel', name: '功能建模',  description: '建立功能模型' },
      // { key: 'functionModelDiagram', name: '功能模型图', description: '查看功能模型图' },
			{ key: 'interactionAnalysis', name: '功能模型图', description: '查看功能模型图' },
      { key: 'functionalFlaw', name: '功能缺陷列表', description: '列出功能缺陷' }
    ]
  },
  triz: {
    title: 'TRIZ工具使用',
    items: [
      { key: 'causalChainAnalysis', name: '因果链分析', description: '进行因果链分析' },
      { key: 'technicalContradiction', name: '技术矛盾', description: '分析技术矛盾' },
      { key: 'physicalContradiction', name: '物理矛盾', description: '分析物理矛盾' },
      { key: 'substanceFieldModel', name: '物场模型', description: '构建物场模型' }
    ]
  },
  solution: {
    title: '方案整理与评价',
    items: [
      { key: 'solutionEvaluationTemplate', name: '方案评价模板', description: '使用方案评价模板' },
      { key: 'solutionExpertEvaluation', name: '方案专家评价', description: '获取专家评价' },
      { key: 'finalSolution', name: '确定最终方案', description: '确定最终实施方案' }
    ]
  },
	achievement: {
		title: '项目成果',
		items: [
			{ key: 'cpyx', name: '产品原型', description: '产品原型' },
			{ key: 'xmcg', name: '项目成果', description: '文章、专利、奖项、合作协议、经济效益、社会效益' },
		]
	}
	
}

// 组件映射 - 已实现的组件
const currentComponentMap = {
  projectForm,
	projectSystem,
  nationalPolicy,
  industryDevelopment,
  painPoints,
  expectedGoals,
	patentQuery,

	functionalAnalysis,
	componentAnalysis,
	componentInteraction,
	functionalModel,
	interactionAnalysis,
	functionalFlaw,
}

// 为未实现的组件创建占位组件
const createPlaceholder = (name) => markRaw({
  template: `<div class="component-placeholder"><h3>${name}</h3><p>该功能正在开发中...</p></div>`
})

// 动态添加占位组件
Object.values(menuConfig).forEach(section => {
  section.items.forEach(item => {
    if (!currentComponentMap[item.key]) {
      currentComponentMap[item.key] = createPlaceholder(item.name)
    }
  })
})

// 响应式状态
const expandedSections = reactive({
  problem: true,
  system: true,
  triz: true,
  solution: true,
	achievement: true
})

const currentState = reactive({
  section: '项目基础信息',
  description: '项目基础信息'
})

// 计算属性
const menuSections = Object.entries(menuConfig).map(([key, config]) => ({
  key,
  title: config.title,
  items: config.items
}))

const currentSection = ref(currentState.section)
const currentDescription = ref(currentState.description)

// 消息数据
const messages = [
  { sender: 'AI助手', content: '您好！我是您的项目AI助手，有什么可以帮助您的吗？', type: 'ai' },
  { sender: '用户', content: '请帮我分析一下当前项目的风险点。', type: 'user' },
  { sender: 'AI助手', content: '根据项目信息分析，主要风险点包括：技术实现难度、资源分配不足、时间进度紧张等。建议加强项目监控和风险管理。', type: 'ai' }
]

// 方法
const toggleSection = (section) => {
  expandedSections[section] = !expandedSections[section]
}

const handleMenuClick = (item) => {
  currentComponent.value = item.key
  currentSection.value = item.name
  currentDescription.value = item.description
}

const sendMessage = () => {
  if (inputMessage.value.trim()) {
    console.log('发送消息:', inputMessage.value)
    inputMessage.value = ''
  }
}
</script>

<style scoped>
.project-edit-container {
  display: flex;
  height: 85vh;
  overflow: auto;
  background: #f5f7fa;
}

/* 左侧菜单样式 */
.left-menu {
  width: 200px;
  background: #fff;
  padding: 20px;
  border-right: 1px solid #e6e6e6;
  box-shadow: 2px 0 4px rgba(0, 0, 0, 0.1);
  overflow-y: auto;
  height: 85vh;
  position: sticky;
  top: 0;
  align-self: flex-start;
}

.menu-section {
  margin-bottom: 20px;
}

.menu-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 0 0 16px 0;
  padding-bottom: 8px;
  border-bottom: 1px solid #ebeef5;
  cursor: pointer;
  transition: all 0.3s;
}

.menu-header:hover {
  color: #409EFF;
}

.menu-header h2 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: #303133;
}

.menu-header .el-icon-arrow-down {
  transition: transform 0.3s;
  color: #c0c4cc;
}

.menu-header .rotate-180 {
  transform: rotate(180deg);
}

.menu-items {
  padding-left: 10px;
}

.menu-item {
  padding: 12px 8px;
  border-bottom: 1px dashed #f0f0f0;
  cursor: pointer;
  transition: all 0.3s;
  border-radius: 4px;
  margin-bottom: 4px;
}

.menu-item:hover {
  background-color: #f5f7fa;
  color: #409EFF;
}

.menu-item.active {
  background-color: #ecf5ff;
  color: #409EFF;
  font-weight: 500;
}

.menu-item:last-child {
  border-bottom: none;
}

/* 右侧内容区域 */
.right-content {
  flex: 1;
  background: #fff;
  min-height: 100vh;
}

.content-panel {
  padding: 24px;
  min-height: 100vh;
}

.content-header {
  margin-bottom: 30px;
  padding-bottom: 20px;
  border-bottom: 1px solid #ebeef5;
  position: sticky;
  top: 0;
  background: #fff;
  z-index: 10;
  padding-top: 10px;
  margin-top: -10px;
}

.content-header h2 {
  margin: 0 0 8px 0;
  font-size: 24px;
  font-weight: 600;
  color: #303133;
}

.content-header p {
  margin: 0;
  color: #909399;
  font-size: 14px;
}

.component-area {
  min-height: 500px;
  position: relative;
  z-index: 1;
}

.placeholder-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 400px;
  color: #909399;
}

.placeholder-content i {
  font-size: 64px;
  margin-bottom: 16px;
  color: #c0c4cc;
}

.placeholder-content p {
  margin: 0;
  font-size: 16px;
}

/* 组件占位样式 */
.component-placeholder {
  padding: 20px;
  background: #f9f9f9;
  border-radius: 8px;
  border: 1px dashed #dcdfe6;
}

.component-placeholder h3 {
  color: #409EFF;
  margin-bottom: 10px;
}

/* AI助手固定触发按钮 */
.ai-trigger-fixed {
  position: fixed;
  right: 20px;
  bottom: 0%;
  transform: translateY(-50%);
  z-index: 999;
}

.ai-trigger-button {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 70px;
  height: 70px;
  background: linear-gradient(135deg, #409EFF, #67C23A);
  border-radius: 50%;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
  transition: all 0.3s;
  color: white;
}

.ai-trigger-button:hover {
  transform: scale(1.1);
  box-shadow: 0 6px 20px rgba(64, 158, 255, 0.4);
}

.ai-trigger-button .ai-icon {
  font-size: 24px;
  margin-bottom: 4px;
}

.ai-trigger-button .ai-text {
  font-size: 12px;
  font-weight: 500;
}

/* 抽屉样式 */
.drawer-content {
  padding: 20px;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.drawer-header {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 15px;
  border-bottom: 1px solid #ebeef5;
  position: relative;
}

.drawer-header .ai-icon {
  width: 50px;
  height: 50px;
  background: linear-gradient(135deg, #409EFF, #67C23A);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 15px;
  color: white;
  font-size: 24px;
}

.drawer-title h3 {
  margin: 0 0 5px 0;
  font-size: 18px;
  font-weight: 600;
}

.ai-description {
  color: #909399;
  font-size: 14px;
  margin: 0;
}

.drawer-close {
  position: absolute;
  right: 0;
  top: 0;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #f5f7fa;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: #909399;
  transition: all 0.3s;
}

.drawer-close:hover {
  background: #409EFF;
  color: white;
}

/* 聊天容器 */
.chat-container {
  flex: 1;
  border: 1px solid #ebeef5;
  border-radius: 8px;
  padding: 15px;
  margin-bottom: 20px;
  overflow-y: auto;
  background: #fafafa;
}

.message {
  margin-bottom: 15px;
  max-width: 85%;
}

.message-header {
  margin-bottom: 5px;
}

.sender {
  font-size: 12px;
  color: #909399;
  font-weight: 500;
}

.message-content {
  padding: 10px 15px;
  border-radius: 12px;
  line-height: 1.5;
  word-wrap: break-word;
}

.ai-message {
  margin-right: auto;
}

.ai-message .message-content {
  background: #f0f7ff;
  border: 1px solid #d0e3ff;
  border-top-left-radius: 4px;
}

.user-message {
  margin-left: auto;
}

.user-message .message-content {
  background: #e8f5e9;
  border: 1px solid #c8e6c9;
  border-top-right-radius: 4px;
}

/* 输入区域 */
.input-area {
  display: flex;
  gap: 10px;
  align-items: center;
}

.input-area :deep(.el-input) {
  flex: 1;
}

.send-button {
  background: linear-gradient(135deg, #409EFF, #67C23A);
  border: none;
  border-radius: 6px;
  padding: 10px 20px;
}

.send-button:hover {
  opacity: 0.9;
}
</style>